package com.cennavi.mineplay.utils;

import java.util.Iterator;

import com.alibaba.fastjson.JSONObject;

public class JsonUtils {

	
	public static String condition2String(String condition){
		
		StringBuilder sb = new StringBuilder();
		
		JSONObject json = JSONObject.parseObject(condition);
		
		Iterator<String> it = json.keySet().iterator();
		
		boolean flag = false;
		
		while(it.hasNext()){
			String key = it.next();
			
			Object objValue = json.get(key);
			
			if (objValue == null || "".equals(objValue.toString()))
				continue;
			
			if (objValue instanceof Number){
				
				String cond = "=";
				
				if (key.startsWith("s_")){
					cond = ">=";
					key = key.substring(2);
				}else if (key.startsWith("e_")){
					cond = "<=";
					key = key.substring(2);
				}
				
				
				if (flag){
					sb.append(" and ");
				}else{
					flag = true;
				}
				sb.append(key + cond+ objValue);
				
			}else{
				if (flag){
					sb.append(" and ");
				}else{
					flag = true;
				}
				sb.append(key + " like '%" + objValue.toString()+"%'");
			}
		}
		
		
		
		
		return sb.toString();
		
	}
	
}
